<?php

namespace App\Http\Controllers;

use App\Huati;
use App\HuatiOwner;
use App\HuatiUserNexus;
use App\Postings;
use App\User;
use Illuminate\Http\Request;

/**
 * Class CommunityController
 * @package App\Http\Controllers
 */
class CommunityController extends Controller
{
    /**
     * 圈主信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function selfInfo(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        return response()->json(User::UserInfo($request->token));
    }


    /**
     * 圈子信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function communityInfo(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }

        return response()->json(Huati::HuatiInfo($huati_id));
    }


    /**
     * 修改圈子信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function communityModify(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }
        return response()->json(Huati::HuatiModify($huati_id, $request->introduce, $request->pic));
    }


    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function userList(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        //校验是不是圈主
        $user_id = hashid_decode($request->token);
        $huati_id = HuatiOwner::IsOwner($user_id);
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }

        return response()->json(HuatiUserNexus::UserList($huati_id, $request->page, $request->user_name, $request->status, $user_id));
    }

    /**
     * 修改用户禁言状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function userStatus(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        if (!$request->has('id')) {
            return response()->json(error('我不知道你在搞什么啊！！'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }
        //校验数据是否正确
        $huati_user_nexus = HuatiUserNexus::find($request->id);
        if (!$huati_user_nexus) {
            return response()->json(error('我不知道你在搞什么啊！！'));
        }
        return response()->json(HuatiUserNexus::Ban($request));
    }


    /**
     * 文章列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postingsList(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }

        return response()->json(Postings::PostingsList($huati_id, $request->page));
    }

    /**
     * 删除帖子
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postingsDel(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        if (!$request->has('archive_id')) {
            return response()->json(error('我不知道你在搞什么啊！！'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }
        //文章是否存在
        if (!Postings::where('archive_id', $request->archive_id)->where('huati_id', $huati_id)->exists()) {
            return response()->json(error('你是在搞笑吗？'));
        }

        if (!Postings::where('archive_id', $request->archive_id)->where('huati_id', $huati_id)->update([
            'is_view' => Postings::VIEW_NO
        ])) {
            return response()->json(error('失败了哦'));
        }

        return response()->json(success(['message' => '删除成功']));
    }

    /**
     * 修改文章加精状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postingsHot(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        if (!$request->has('archive_id')) {
            return response()->json(error('我不知道你在搞什么啊！！'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }
        //文章是否存在
        if (!is_numeric($archive_hot = Postings::where('archive_id', $request->archive_id)->where('huati_id', $huati_id)->value('is_hot'))) {
            return response()->json(error('你是在搞笑吗？'));
        }

        //修改加精状态
        $is_hot = $archive_hot === Postings::HOT_YES ? Postings::HOT_NO : Postings::HOT_YES;

        if (!Postings::where('archive_id', $request->archive_id)->where('huati_id', $huati_id)->update([
            'is_hot' => $is_hot
        ])) {
            return response()->json(error('失败了哦'));
        }

        return response()->json(success(['message' => '成功']));
    }

    /**
     * 举报信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function report(Request $request)
    {
        if (!$request->has('token')) {
            return response()->json(error('没有token'));
        }
        //校验是不是圈主
        $huati_id = HuatiOwner::IsOwner(hashid_decode($request->token));
        if (!$huati_id) {
            return response()->json(error('您不是圈主哦'));
        }
        //举报信息
        $list = Postings::Report($huati_id);
        return response()->json($list);
    }
}
